<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>修改角色</title>
	<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	<script src="../../../component/layui/layui.js"></script>
	<script src="../../../component/pear/pear.js"></script>
</head>

<body>
	<form class="layui-form" action="" lay-filter="form" autocomplete="off">
		<div class="mainBox">
			<div class="main-container">
				<div class="layui-form-item">
					<label class="layui-form-label">角色名称<span class="layui-font-red">*</span></label>
					<div class="layui-input-block">
						<input type="text" name="name" lay-verify="required" placeholder="请输入角色名称" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">角色排序<span class="layui-font-red">*</span></label>
					<div class="layui-input-block">
						<input type="number" name="sort" lay-verify="required" placeholder="请输入角色排序"
							class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">是否有效<span class="layui-font-red">*</span></label>
					<div class="layui-input-block">
						<input type="checkbox" name="isActive" lay-skin="switch" lay-text="启用|禁用" lay-filter="isActive">
					</div>
				</div>
			</div>
		</div>
		<div class="bottom">
			<div class="button-container">
				<button type="submit" class="pear-btn pear-btn-sm pear-btn-primary" lay-submit="" lay-filter="save">
					<i class="icon pear-icon pear-icon-select-bold"></i>
					提交
				</button>
				<button type="button" lay-click="layerclose" class="pear-btn pear-btn-sm">
					<i class="icon pear-icon pear-icon-close-bold"></i>
					关闭
				</button>
			</div>
		</div>
	</form>
</body>

</html>

<script>
	layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'common', 'button', 'util'], function () {
		var form = layui.form;
		var $ = layui.jquery;
		var context = layui.context;
		var popup = layui.popup;
		var common = layui.common;
		var button = layui.button;
		var util = layui.util;

		var basePath = context.get("base-path");
		var currentId = common.getQueryString("id");

		$.ajax({
			url: basePath + "/api/system/role/getinfo",
			data: JSON.stringify({ Id: currentId }),
			success: function (result) {
				form.val("form", {
					name: result.data.name,
					sort: result.data.sort,
					isActive: result.data.isActive,
				});
			}
		})

		form.on("submit(save)", function (data) {
			var btn = button.load({ elem: "button[lay-filter=save]" });
			data.field.id = currentId
			data.field.isActive = data.field.isActive == "on" ? true : false;
			$.ajax({
				url: basePath + "/api/system/role/edit",
				data: JSON.stringify(data.field),
				success: function (result) {
					btn.stop(function () {
						if (result.succeeded) {
							popup.success("修改成功", function () {
								parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
								parent.layui.table.reload("table");
							});
						} else {
							popup.failure(JSON.stringify(result.errors));
						}
					})
				}
			})
			return false;
		});


		util.event('lay-click', {
			layerclose: function (othis) {//关闭当前弹窗
				parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
			},
		});
	})
</script>